<template>
	<view class="QS-CountDown" ref="QS-CountDown" :class="getClass" :style="getStyle">
		<block v-if="['day'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{countDownObj.day || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">:</text>
		</block>
		<block v-if="['day', 'hour'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{countDownObj.hour || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">:</text>
		</block>
		<block v-if="['day', 'hour', 'minute'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{countDownObj.minute || '00'}}</text>
			</view>
			<text class="count-down-item-placeholder" :style="{ color: backgroundColor }">:</text>
		</block>
		<block v-if="['day', 'hour', 'minute', 'second'].includes(fields)">
			<view class="count-down-item" :style="{
				padding: padding,
				backgroundColor: backgroundColor
			}">
				<text class="count-down-item-text" style="color: #FFFFFF;">{{countDownObj.second || '00'}}</text>
			</view>
		</block>
	</view>
</template>

<script>
	import QSComponentMixin from '../../mixins/QS-Components-Mixin.js';
	const QSComponentMixinRes = QSComponentMixin({ 
		componentType: 'QS-Canvas',
		nodeName: '.QS-Canvas'
	});
	let props;
	try {
		props = require('@/QS-UI-CONFIG/components/QS-CountDown/js/props.js');
	} catch (e) {
		//TODO handle the exception
		props = {};
	}
	
	export default {
		mixins: [QSComponentMixinRes.mixin],
		props: {
			// #ifdef MP-ALIPAY
			...QSComponentMixinRes.props,
			// #endif
			padding: {
				type: String,
				default: '8rpx'
			},
			time: {
				type: [Number, String],
				default: +new Date()
			},
			backgroundColor: {
				type: String,
				default: '#F52F50'
			},
			id: {
				type: String,
				default: ''
			},
			fields: {
				type: String,
				default: 'day'
			},
			...props
		},
		watch: {
			time() {
				this.init();
			}
		},
		data() {
			return {
				countDownObj: {}
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			init() {
				this.countDownObj = uni.$qs.countDown({
					id: this.id,
					beginDate: this.time,
					fields: this.fields
				});
			}
		}
	}
</script>

<style scoped>
	.QS-CountDown{
		display: flex;
		flex-direction: row;
		white-space: nowrap;
	}
	.count-down-item{
		border-radius: 4rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		height: 36rpx;
		width: 36rpx;
	}
	.count-down-item-text{
		font-size: 22rpx;
		font-weight: bold;
	}
	.count-down-item-placeholder{
		font-size: 22rpx;
		margin-left: 8rpx;
		margin-right: 8rpx;
	}
</style>
